var md5 = require('./md5.js');
function refreshToken(){
    wx.request({
        url: 'https://admin.our-dream.cn/api/58feec00daad5',
        data: getSign(),
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
            version:'v2.0',
            agent:'wx'
        },
        success: function(res){
            wx.setStorageSync('ApiAdminAccessToken', res.data.data.access_token);
        }
    });
}

function randomString(len) {
　　len = len || 32;
　　var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
　　var maxPos = $chars.length;
　　var pwd = '';
　　for (var i = 0; i < len; i++) {
　　　　pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
　　}
　　return pwd;
}

function getSign() {
    var secret = 'cjBoPgZVGkHMfBRBOtfjzPOSLNiULSWa';
    var data = {
        timestamp:new Date().getTime(),
        app_id:'95835262',
        rand_str:randomString(18),
        device_id:'wx'
    }
    var preStr = 'app_id='+ data.app_id +'&app_secret='+ secret 
        +'&device_id='+data.device_id+'&rand_str='+data.rand_str+'&timestamp='+data.timestamp;
    data.signature = md5.hexMD5(preStr);
    return data;
}

module.exports = {
  refreshToken: refreshToken
}